package com.lemlin.network

import androidx.annotation.CallSuper
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils

open class ViewModelSubscribe<HttpData : HttpServiceData>(
    private val liveData: MutableLiveData<HttpServiceResponse<HttpData>>?
) : SubscribeListener<HttpData> {
    @CallSuper
    override fun onStart() {
        with(liveData?.value ?: HttpServiceResponse()) {
            statusCode = HttpServiceStatus.Status.START
            liveData?.postValue(this)
        }
    }

    override fun onSuccess(resp: HttpData) {
        with(liveData?.value ?: HttpServiceResponse()) {
            statusCode = HttpServiceStatus.Status.SUCCESS
            httpData = resp
            liveData?.postValue(this)
        }
    }

    @CallSuper
    override fun onFailure(throwable: Throwable) {
        with(liveData?.value ?: HttpServiceResponse()) {
            this.statusCode = HttpServiceStatus.Status.FAILURE
            this.throwable = throwable
            liveData?.postValue(this)
        }
    }
}